package com.example.mapsapp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

public class PostRegisterTask extends AsyncTask <String, String, Boolean> {

	@Override
	protected Boolean doInBackground(String... args) {
		// TODO Auto-generated method stub
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://softeng.cs.uwosh.edu/students/cs342g2/register.php");

		try {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("user", args[0]));
			nameValuePairs.add(new BasicNameValuePair("password", args[1]));
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			System.out.println(EntityUtils.toString(httppost.getEntity()));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			if(entity!= null)
			{
				 InputStream instream = entity.getContent();
				 String result = instream.toString();
				 Log.i("Read from server", result);
				 return true;
			}
             InputStream instream = entity.getContent();
		   
			// result = EntityUtils.toString(response.getEntity());
		} 
		catch (ClientProtocolException e)
		{
            e.printStackTrace();
		} 
		catch (IOException e)
       {
        e.printStackTrace();
		}
		  return null;

	}
	
	protected void onPostExecute(Boolean result)
	{
		
	}

}
